<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="对应CODEID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['code']" placeholder="请输入对应CODEID"></a-input>
        </a-form-item>
        <a-form-item label="是否是字典类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['izDict']" :trigger-change="true" dictCode="yn" placeholder="请选择是否是字典类型"/>
        </a-form-item>
        <a-form-item label="字典编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-search-select-tag v-decorator="['dictCode']" dict="sys_dict,dict_name,dict_code" />
        </a-form-item>
        <a-form-item label="字典对应的值" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['dictItemValue']" :trigger-change="true" dictCode="zcgl_contract_status" placeholder="请选择字典对应的值"/>
        </a-form-item>
        <a-form-item label="类型：1、自动编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['type']" :trigger-change="true" dictCode="encoding_type" placeholder="请选择类型：1、自动编码"/>
        </a-form-item>
        <a-form-item label="前缀1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['prefix1']" placeholder="请输入前缀1"></a-input>
        </a-form-item>
        <a-form-item label="前缀1格式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['prefix1Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="请选择前缀1格式"/>
        </a-form-item>
        <a-form-item label="前缀1长度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['prefix1Length']" placeholder="请输入前缀1长度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="前缀2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['prefix2']" placeholder="请输入前缀2"></a-input>
        </a-form-item>
        <a-form-item label="前缀2格式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['prefix2Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="请选择前缀2格式"/>
        </a-form-item>
        <a-form-item label="前缀2长度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['prefix2Length']" placeholder="请输入前缀2长度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="前缀3" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['prefix3']" placeholder="请输入前缀3"></a-input>
        </a-form-item>
        <a-form-item label="前缀3格式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['prefix3Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="请选择前缀3格式"/>
        </a-form-item>
        <a-form-item label="前缀3长度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['prefix3Length']" placeholder="请输入前缀3长度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="后缀1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['suffix1']" placeholder="请输入后缀1"></a-input>
        </a-form-item>
        <a-form-item label="后缀1格式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['suffix1Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="请选择后缀1格式"/>
        </a-form-item>
        <a-form-item label="后缀1长度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['suffix1Length']" placeholder="请输入后缀1长度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="后缀2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['suffix2']" placeholder="请输入后缀2"></a-input>
        </a-form-item>
        <a-form-item label="后缀2格式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['suffix2Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="请选择后缀2格式"/>
        </a-form-item>
        <a-form-item label="后缀2长度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['suffix2Length']" placeholder="请输入后缀2长度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="后缀3" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['suffix3']" placeholder="请输入后缀3"></a-input>
        </a-form-item>
        <a-form-item label="后缀3格式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['suffix3Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="请选择后缀3格式"/>
        </a-form-item>
        <a-form-item label="后缀3长度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['suffix3Length']" placeholder="请输入后缀3长度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="编码预览" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['coding']" placeholder="请输入编码预览"></a-input>
        </a-form-item>
        <a-form-item label="当前值" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['codingNow']" placeholder="请输入当前值"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'


  export default {
    name: "EncodingRulesModal",
    components: { 
      JDictSelectTag,
      JSearchSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/system/encodingRules/add",
          edit: "/system/encodingRules/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'code','izDict','dictCode','dictItemValue','type','prefix1','prefix1Type','prefix1Length','prefix2','prefix2Type','prefix2Length','prefix3','prefix3Type','prefix3Length','suffix1','suffix1Type','suffix1Length','suffix2','suffix2Type','suffix2Length','suffix3','suffix3Type','suffix3Length','coding','codingNow'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'code','izDict','dictCode','dictItemValue','type','prefix1','prefix1Type','prefix1Length','prefix2','prefix2Type','prefix2Length','prefix3','prefix3Type','prefix3Length','suffix1','suffix1Type','suffix1Length','suffix2','suffix2Type','suffix2Length','suffix3','suffix3Type','suffix3Length','coding','codingNow'))
      },

      
    }
  }
</script>